package cn.wolfcode.luowowo.service.impl;

import cn.wolfcode.luowowo.domain.Strategy;
import cn.wolfcode.luowowo.domain.StrategyComment;
import cn.wolfcode.luowowo.domain.UserInfo;
import cn.wolfcode.luowowo.query.StrategyCommentQuery;
import cn.wolfcode.luowowo.repository.StrategyCommentRepository;
import cn.wolfcode.luowowo.service.IStrategyCommentService;
import cn.wolfcode.luowowo.service.IStrategyService;
import cn.wolfcode.luowowo.util.DBHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;
import java.util.regex.Pattern;

@Service
public class StrategyCommentServiceImpl implements IStrategyCommentService {

    @Autowired
    private StrategyCommentRepository strategyCommentRepository;

    @Autowired
    private MongoTemplate template;
    @Autowired
    private IStrategyService strategyService;

    @Override
    public StrategyComment get(String id) {
        return strategyCommentRepository.findById(id).orElse(null);
    }

    @Override
    public List<StrategyComment> listAll() {
        return strategyCommentRepository.findAll();
    }

    @Override
    public void delete(String id) {
        strategyCommentRepository.deleteById(id);
    }

    @Override
    public void save(StrategyComment strategyComment) {
        strategyComment.setId(null);
        String content = strategyComment.getContent();
        String regEx = "[日操傻逼]";
        content = Pattern.compile(regEx).matcher(content).replaceAll("*").trim();
        strategyComment.setContent(content);
        strategyCommentRepository.save(strategyComment);
    }

    @Override
    public void update(StrategyComment strategyComment) {
        strategyCommentRepository.save(strategyComment);
    }

    @Override
    public Page<StrategyComment> query(StrategyCommentQuery qo) {
        //查询条件
        Query query = new Query();
        //高级查询条件
        if(StringUtils.hasLength(qo.getStrategyId())){
            query.addCriteria(Criteria.where("strategyId").is(qo.getStrategyId()));
        }
        PageRequest pageable = PageRequest.of(qo.getCurrentPage() - 1, qo.getPageSize(),
                Sort.Direction.DESC, "createTime");
        Page<StrategyComment> page = DBHelper.query(template, StrategyComment.class, query, pageable);

        return page;
    }

    @Override
    public void commentThumb(String cid, String uid) {
        //获取当前评论用户的点赞集合
        StrategyComment strategyComment = this.get(cid);
        List<String> thumbuplist = strategyComment.getThumbuplist();
        //再进行遍历
        if(thumbuplist.contains(uid)){//有点赞时(在评论的点赞集合中存在该用户id)
            //则进行删除操作
            thumbuplist.remove(uid);
            strategyComment.setThumbupnum(thumbuplist.size());
        }else{//没有点赞时(在评论的点赞集合中没有该用户id)
            //则进行添加操作
            thumbuplist.add(uid);
            strategyComment.setThumbupnum(thumbuplist.size());
        }
        this.update(strategyComment);
    }

    @Override
    public List<StrategyComment> getStragegyNum(String strategyId) {
        return strategyCommentRepository.findByStrategyId(strategyId);
    }


}
